import OpenGL.GL as gl
import numpy as np

class VolumeTexture3D:
    def __init__(self, volume:np.ndarray) -> None:
        tex = np.empty(1, gl.GLuint)
        gl.glCreateTextures(gl.GL_TEXTURE_3D, 1, tex)
        self.tex_id = tex[0]
        self.pixel_format = gl.GL_RED
        self.internal_format = gl.GL_R8
        depth, height, width = volume.shape 
        gl.glTextureStorage3D(self.tex_id, 1, gl.GL_R8, width, height, depth) 
        gl.glTextureSubImage3D(self.tex_id, 0, 0, 0, 0, width, height, depth,
                gl.GL_RED, gl.GL_UNSIGNED_BYTE, volume.data)
        gl.glGenerateTextureMipmap(self.tex_id)

    def bind(self, index: int) -> None:
        gl.glBindTextureUnit(index, self.tex_id)

    def delete(self) -> None:
        if gl.glIsTexture(self.tex_id):
            gl.glDeleteTextures(1, self.tex_id)

    def setFiltering(self, min_filter: int, mag_filter: int) -> None:
        gl.glTextureParameteri(self.tex_id, gl.GL_TEXTURE_MIN_FILTER, min_filter)
        gl.glTextureParameteri(self.tex_id, gl.GL_TEXTURE_MAG_FILTER, mag_filter)

    def setWrapMode(self, wrap_s: int, wrap_t: int, wrap_r:int) -> None:
        gl.glTextureParameteri(self.tex_id, gl.GL_TEXTURE_WRAP_S, wrap_s)
        gl.glTextureParameteri(self.tex_id, gl.GL_TEXTURE_WRAP_T, wrap_t)
        gl.glTextureParameteri(self.tex_id, gl.GL_TEXTURE_WRAP_R, wrap_r)

